package com.gandong8.es.web.config;


import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeetlConf {
    @Value("${beetl.templates.path:templates}")
    String templatesPath;
    @Value("${beetl.templates.suffix:.html}")
    String templatesSuffix;

    @Bean(name = "beetlConfig")
    public BeetlGroupUtilConfiguration getConfig() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if(classLoader == null) {
            classLoader = BeetlConf.class.getClassLoader();
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader, templatesPath);
        beetlGroupUtilConfiguration.setResourceLoader(resourceLoader);
        beetlGroupUtilConfiguration.init();
        beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(classLoader);
        return beetlGroupUtilConfiguration;
    }

    @Bean
    public BeetlSpringViewResolver getResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration configuration) {
        BeetlSpringViewResolver resolver = new BeetlSpringViewResolver();
        resolver.setContentType("text/html;charset=UTF-8");
        resolver.setOrder(0);
        resolver.setConfig(configuration);
        resolver.setSuffix(templatesSuffix);
        return resolver;
    }

}

